﻿#include "screen.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QFile>
#include <QDir>
#include <QLabel>
#include <QStringList>
#include <QTextStream>
#include <QDialog>
#include <QHideEvent>

PlayScreen::PlayScreen() :
    Screen()
{
    descLabel = new QLabel();
    descLabel->setMinimumWidth(300);
    descLabel->setMaximumWidth(300);
    descLabel->setWordWrap(true);
    layoutLeft->addWidget(descLabel);

    buttonSave->setText(QString::fromUtf8("Tạm Dừng"));
    buttonSave->setDisabled(true);
    buttonDone->setText(QString::fromUtf8("Chấm điểm"));
    buttonDone->setDisabled(true);
    grid = new PlayGrid(this);
    QGraphicsView* view = new QGraphicsView(this);
    view->setMinimumWidth(550);
    view->setMaximumWidth(550);
    view->setScene(grid);
    layoutNew->addWidget(view);
}

void PlayScreen::ShowSelectWindow(bool type)
{
    QDialog* selectWindow = new QDialog();
    selectWindow->setAttribute(Qt::WA_DeleteOnClose);
    selectWindow->setMaximumWidth(400);
    QVBoxLayout* layout = new QVBoxLayout(selectWindow);
    QDir dir;

    QString path;
    if(type)
    {
        path = QString("CrossWord/Grid/");
    }else{
        path = QString("CrossWord/Game/");
    }


    if(!dir.exists(path))
    {
        QLabel* label = new QLabel(QString::fromUtf8("Không tìm thấy thư mục mặc định chứa ô chử (%1).\nHãy dùng chức năng tạo mới ô chử của riêng bạn.").arg(path), selectWindow);
        layout->addWidget(label);
    } else {
        dir.setPath(path);
        QStringList list = dir.entryList(QDir::Files);
        if(list.size() < 1)
        {
            QLabel* label = new QLabel(QString::fromUtf8("Chưa có ô chử nào đựợc tìm thấy,\nhảy tự tạo mới ô chử."), selectWindow);
            layout->addWidget(label);
        } else {
            for(int i = 0; i < list.size(); i++)
            {
                QFile file(list.value(i).prepend(path));
                if (!file.open( QIODevice::ReadOnly ))
                {
                    QLabel* label = new QLabel(QString::fromUtf8("Không thể mở file: ").append(list.value(i)), selectWindow);
                    label->setStyleSheet("color: blue; background-color: yellow");
                    layout->addWidget(label);
                } else {
                    QString desc;
                    QTextStream stream(&file);
                    stream.setCodec("UTF-8");
                    desc = stream.readLine();

                    QLabel* label = new QLabel(QString("<a href='%1'>").arg(list.value(i).prepend(path)).append(desc).append("</a>"));
                    connect(label, SIGNAL(linkActivated(QString)), grid, SLOT(Load(QString)));
                    layout->addWidget(label);
                }
                file.close();
            }
        }
    }
    QPushButton* closebutton = new QPushButton("Close", selectWindow);
    connect(closebutton, SIGNAL(clicked()), selectWindow, SLOT(deleteLater()));
    layout->addWidget(closebutton);
    selectWindow->setLayout(layout);
    selectWindow->exec();
}
